//
//  WFSearchViewController.m
//  谷居
//
//  Created by dllo on 16/7/2.
//  Copyright © 2016年 刘万方. All rights reserved.
//

#import "WFSearchViewController.h"
#import "WFTableView.h"
#import "WFButtonText.h"
#import "WFSearchCell.h"
#import "MBProgressHUD.h"
#import "NetWorkingTool.h"
#import "Masonry.h"
#import "AFNetworking.h"
@interface WFSearchViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) WFTableView *tableView;

@property (nonatomic, retain) WFButtonText *leftBarButton;

@property (nonatomic, retain) WFButtonText *rightBarButton;

@property (nonatomic, retain) NSMutableArray *searchArray;

@property (nonatomic, retain) MBProgressHUD *hud;


@end

@implementation WFSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /** 创建tableView. */
    [self createTableView];
    /** 创建小菊花. */
    [self createHUD];
    
    
    /** 创建数据. */
    [self createData];
    
    [self post];
    // Do any additional setup after loading the view.
}

- (void)post {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *str = [@"%E5%A4%A9" stringByRemovingPercentEncoding];
    NSLog(@"%@", str);
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"buildingName"] = @"天";
    params[@"cityId"] = @"441";
    params[@"p"] = @"0";
    NSLog(@"%@", params);
    NSString *qe = @"buildingName=%E5%95%A6%E5%95%A6&cityId=441&p=0";
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager POST:@"http://guju.com.cn/building/building_searchMore_ajax" parameters:qe progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

}

- (void)createTableView {
    _tableView = [[WFTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    [_tableView release];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.searchArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"search";
    WFSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell  = [[[WFSearchCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse]autorelease];
    }
    return cell;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    UIImageView *imageVeiw = [[UIImageView alloc] init];
    [view addSubview:imageVeiw];
    [imageVeiw release];
    [imageVeiw mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.equalTo(view).offset(10);
        make.bottom.equalTo(view).offset(-10);
        make.width.equalTo(imageVeiw.mas_height);
    }];
    imageVeiw.image = [UIImage imageNamed:@"竖线2.png"];

    UILabel *label = [[UILabel alloc] init];
    [view addSubview:label];
    [label release];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(imageVeiw.mas_right).offset(10);
        make.top.equalTo(view).offset(10);
        make.bottom.equalTo(view).offset(-10);
        make.width.greaterThanOrEqualTo(@60);
        
    }];
    [label sizeToFit];
    label.text = @"热门搜索";
    
    
    return view;
}


- (void)createHUD {
    _hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    _hud.mode = MBProgressHUDAnimationFade;
    _hud.labelText = @"刷新中";
}


- (void)createData {
    [NetWorkingTool netWorkingWithURL:@"http://api.guju.com.cn/v2/building/recommend?cityId=441&start=1" block:^(id result) {
        
        [_hud hide:YES];
        
    }];
}
#pragma mark - 懒加载
- (NSMutableArray *)searchArray {
    if (_searchArray == nil) {
        _searchArray = [[NSMutableArray array]retain];
    }
    return _searchArray;
}
- (void)dealloc {
    [_searchArray release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
    
}
-(void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
